from django.db import models

from accounts.models import CustomUser
from assets.models import Host


class AuthGroup(models.Model):
    """
    权限组
    """
    group_name = models.CharField(max_length=100, verbose_name='角色名称', unique=True)
    group_user = models.ManyToManyField(CustomUser, blank=True, verbose_name='所属用户')
    enable = models.BooleanField(default=True, verbose_name='是否启用')
    explanation = models.TextField(verbose_name='角色描述')
    date_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.group_name

    class Meta:
        verbose_name = "角色管理"
        verbose_name_plural = verbose_name


class UserAuthCmdb(models.Model):
    """
    cmdb权限
    所有字段全部以0，1来表示
    1表示有此权限，0表示无此权限
    所有数据全部外键关联user表，当用户删除时相应权限也随之删除
    """

    select_host = models.BooleanField(default=False, verbose_name="查看资产")
    edit_host = models.BooleanField(default=False, verbose_name="修改资产")
    update_host = models.BooleanField(default=False, verbose_name="更新资产")
    add_host = models.BooleanField(default=False, verbose_name="添加主机")
    bat_add_host = models.BooleanField(default=False, verbose_name="批量添加")
    delete_host = models.BooleanField(default=False, verbose_name="删除资产")
    add_line_auth = models.BooleanField(default=False, verbose_name="产品线管理")
    """
    发布权限
    """
    auth_project = models.BooleanField(default=False, verbose_name="自动化发布")
    auth_highstate = models.BooleanField(default=False, verbose_name="自动化部署")

    """
    用户管理
    """
    add_user = models.BooleanField(default=False, verbose_name='添加用户')
    edit_user = models.BooleanField(default=False, verbose_name='修改用户')
    edit_pass = models.BooleanField(default=False, verbose_name="修改密码")
    delete_user = models.BooleanField(default=False, verbose_name="删除用户")
    add_department = models.BooleanField(default=False, verbose_name="部门管理")

    """
    机房管理
    """

    select_idc = models.BooleanField(default=False, verbose_name="查看机房")
    add_idc = models.BooleanField(default=False, verbose_name="添加机房")
    edit_idc = models.BooleanField(default=False, verbose_name="修改机房")
    del_idc = models.BooleanField(default=False, verbose_name="删除机房")

    """
    系统管理
    """
    setup_system = models.BooleanField(default=False, verbose_name="安装系统")
    upload_system = models.BooleanField(default=False, verbose_name="主机上报")
    salt_keys = models.BooleanField(default=False, verbose_name="主机上报")

    """
    项目管理
    """
    project_auth = models.BooleanField(default=False, verbose_name="项目列表")
    add_project = models.BooleanField(default=False, verbose_name="添加项目")
    edit_project = models.BooleanField(default=False, verbose_name="修改项目")
    delete_project = models.BooleanField(default=False, verbose_name="删除项目")

    """
    日志管理
    """
    auth_log = models.BooleanField(default=False, verbose_name="salt执行记录")
    cmdb_log = models.BooleanField(default=False, verbose_name="资产操作记录")
    server_audit = models.BooleanField(default=False, verbose_name="服务器操作记录")

    group_name = models.ForeignKey(AuthGroup, verbose_name='所属角色', help_text="添加角色组权限", on_delete=models.CASCADE)

    def __str__(self):
        return self.group_name
        # return "权限管理"

    class Meta:
        verbose_name = "权限管理"
        verbose_name_plural = verbose_name


class AuthSudo(models.Model):
    groupname = models.CharField(max_length=64, verbose_name="组名", help_text="sudo组")
    shell = models.TextField(verbose_name='命令')
    datetime = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.groupname

    class Meta:
        managed = True
        db_table = 'auth_sudo'
        verbose_name = "sudo授权"
        verbose_name_plural = verbose_name


class AuthNode(models.Model):
    user_name = models.ForeignKey(CustomUser, max_length=20, verbose_name="名称", help_text="用户",
                                  on_delete=models.CASCADE)
    node = models.ForeignKey(Host, null=True, blank=True, verbose_name='主机', on_delete=models.CASCADE)
    auth = models.BooleanField(verbose_name='是否管理员', default=0)
    datetime = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.user_name

    class Meta:
        managed = True
        db_table = 'auth_node'
        verbose_name = "主机权限"
        verbose_name_plural = verbose_name
